package np.com.baralsujan.algorithms;

public class QuickSortInline {
	
	private int [] srcArray;
	
	public QuickSortInline(int [] srcArray) {
		this.srcArray = srcArray;
	}
	
	public void sort() {
		quickSort(0, srcArray.length - 1);
	}
	
	private void quickSort(int low, int high) {
		int pivot = low + (high - low) / 2;
		
		int i = low;
		while(i <= high) {
			System.out.println("src:" + srcArray[i] + ", pivot: "+ srcArray[pivot]);
			if(srcArray[i] > srcArray[pivot]) {
				srcArray[i] = srcArray[i] - srcArray[pivot]; 
				srcArray[pivot] = srcArray[i] + srcArray[pivot]; 
				srcArray[i] = srcArray[pivot] - srcArray[i];
				pivot = i;
			}
			i++;
		}
	}
	
	public void print() {
		for (int i = 0; i < srcArray.length; i++) {
			System.out.print(srcArray[i] + ",");
		}
	}

	public static void main(String[] args) {
		int [] array = {3, 5, 2, 1, 6, 4, 9, 4, 7};
		QuickSortInline inline = new QuickSortInline(array);
		inline.sort();
		inline.print();
	}

}
